/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.gyhj.questionaires.handler;

/**
 * @author lengleng
 * @date 2018/5/24
 */

import com.gyhj.questionaires.config.Response;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局的的异常拦截器
 *
 * @author lengleng
 * @date 2018/05/22
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Response exception(Exception e) {
        log.info("保存全局异常信息 ex={}", e.getMessage(), e);
		return Response.failed("保存全局异常信息", e.getMessage());
    }

    /**
     * 业务异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(com.gyhj.questionaires.handler.BusinessException.class)
    @ResponseBody
    public Response businessException(com.gyhj.questionaires.handler.BusinessException e) {
        log.info("保存全局异常信息 ex={}", e.getMessage(), e);
        return Response.failed("保存全局异常信息", e.getMessage());
    }


    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Response MethodArgumentNotValidHandler(MethodArgumentNotValidException e) {
        List<FieldError> list = e.getBindingResult().getFieldErrors();
        return excuteValidException(e, list);
    }

    @ExceptionHandler(value = BindException.class)
    public Response MethodArgumentNotValidHandler(BindException e) {
        List<FieldError> list = e.getBindingResult().getFieldErrors();
        return excuteValidException(e, list);
    }

    private Response excuteValidException(Exception e, List<FieldError> list) {
        //按需重新封装需要返回的错误信息
        StringBuilder sb = new StringBuilder();
        //解析原错误信息，封装后返回，此处返回非法的字段名称，错误信息
        for (FieldError error : list) {
            sb.append("入参字段").append(error.getField()).append(",").append(error.getDefaultMessage()).append(";");
        }
        log.info("保存全局异常信息 ex={}", e.getMessage(), e);
        return Response.failed(sb.toString(), e.getMessage());
    }

}
